from django.shortcuts import render, reverse, redirect
from django.http import HttpResponse
from .forms import *
from .models import *
from common.models import *
from django.views.generic import View
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from FawsnSuggestSystem import settings

# 引入发送邮件的模块
from toolkit.sendmail import SendMail


class SuggestView(View):
    @staticmethod
    @login_required
    def get(request):
        obj = SuggestForm()
        return render(request, 'suggest/suggest.html', context={
            'obj': obj
        })

    @staticmethod
    @login_required
    def post(request):
        obj = SuggestForm(request.POST)
        if obj.is_valid():
            values = obj.cleaned_data
            create_user = request.user
            values['create_user'] = create_user
            values['state'] = 2
            flow = Flow.objects.filter(is_active=True).first()
            flow_node = FlowNode.objects.filter(flow=flow).order_by('order').first()
            current_node = flow_node.next_node
            if not current_node:
                return HttpResponse('审批流程下个节点没有设置,联系管理进行设置')
            values['current_node'] = current_node

            suggest = Suggest.objects.create(**values)
            # 用户提交算流程的第一步.用户提交,需要在审批流程中加入
            operation = OperationOption.objects.filter(name=1).first()

            SuggestAudit.objects.create(operation=operation, suggest=suggest, flow_node=flow_node,
                                        audit_user=create_user)
            return redirect('/suggest_audit/?suggest_id=%s' % (suggest.id))
        else:
            return render(request, 'suggest/suggest.html', context={
                'obj': obj
            })


class SuggestAuditView(View):
    @staticmethod
    @login_required
    def get(request):
        suggest_id = request.GET.get('suggest_id')
        suggest = Suggest.objects.filter(id=suggest_id).first()
        department = suggest.create_user.department
        member = User.objects.filter(department=department)
        #  审批流程
        flow = Flow.objects.filter(is_active=True).first()
        flow_nodes = FlowNode.objects.filter(flow=flow).order_by('order')
        print(flow_nodes)
        flow_node_list = []
        for flow_node in flow_nodes:
            flow_node_data = {}
            flow_node_data['node_name'] = flow_node.flow_node_name
            # 这里需要判断角色的类型,如果是指定审批人则使用users,否则要计算获取待审批人
            flow_node_data['flow_node_roles'] = flow_node.flow_role.users.all()
            flow_audit = SuggestAudit.objects.filter(suggest=suggest, flow_node=flow_node).first()
            if flow_audit:
                flow_node_data['flow_audit'] = True
                flow_node_data['flow_audit_operation'] = flow_audit.operation
                flow_node_data['flow_audit_create_user'] = flow_audit.audit_user
                flow_node_data['flow_audit_suggest'] = flow_audit.audit_suggest
                flow_node_data['flow_audit_time'] = flow_audit.audit_time
            else:
                flow_node_data['flow_audit'] = False
            flow_node_list.append(flow_node_data)
        current_node = suggest.current_node
        current_role_users = current_node.flow_role.users.all()
        user = request.user
        flow_audit = {}
        if user in current_role_users:
            flow_audit['is_audit'] = True
            flow_audit['operations'] = current_node.flow_operation.all()
        else:
            flow_audit['is_audit'] = False
        return render(request, 'suggest/suggest_audit.html', context={
            'flow_node_list': flow_node_list,
            'suggest': suggest,
            'member': member,
            'flow_audit': flow_audit
        })

    @staticmethod
    @login_required
    def post(request):
        operation_id = request.POST.get('operation')
        operation = OperationOption.objects.get(id=operation_id)
        audit_suggest = request.POST.get('audit_suggest')
        audit_user = request.user
        flow_node_id = request.POST.get('flow_node')
        flow_node = FlowNode.objects.get(id=flow_node_id)
        suggest_id = request.POST.get('suggest')
        suggest = Suggest.objects.get(id=suggest_id)
        # 审批结果为同意
        if operation.name == 2:
            SuggestAudit.objects.create(operation=operation, audit_suggest=audit_suggest, audit_user=audit_user,
                                        flow_node=flow_node, suggest=suggest)
            # 如果没有下个节点,说明流程结束
            if flow_node.next_node:
                suggest.current_node = flow_node.next_node
            else:
                suggest.state = 3
            suggest.save()
            return redirect('/suggest_audit/?suggest_id=%s' % (suggest.id))
        # 用户撤回
        elif operation.name == 3:
            suggest.state = 5
            suggest.save()
            SuggestAudit.objects.create(operation=operation, audit_suggest=audit_suggest, audit_user=audit_user,
                                        flow_node=flow_node, suggest=suggest)
            return redirect('/suggest_audit/?suggest_id=%s' % (suggest.id))
        # 驳回
        elif operation.name == 4:
            suggest.state = 5
            suggest.save()
            SuggestAudit.objects.create(operation=operation, audit_suggest=audit_suggest, audit_user=audit_user,
                                        flow_node=flow_node, suggest=suggest)
            return redirect('/suggest_audit/?suggest_id=%s' % (suggest.id))
        else:
            return HttpResponse(operation)


class SuggestListView(View):
    @staticmethod
    @login_required
    def get(request):
        user = request.user
        # 审批中
        suggest2_list = Suggest.objects.filter(create_user=user, state=2)
        # 审批结束
        suggest3_list = Suggest.objects.filter(create_user=user, state=3)
        # 用户撤回
        suggest4_list = Suggest.objects.filter(create_user=user, state=4)
        # 被驳回
        suggest5_list = Suggest.objects.filter(create_user=user, state=5)
        return render(request, 'suggest/suggest_list.html', context={
            'suggest2_list': suggest2_list,
            'suggest3_list': suggest3_list,
            'suggest4_list': suggest4_list,
            'suggest5_list': suggest5_list,
        })


class SuggestAuditListView(View):
    @staticmethod
    @login_required
    def get(request):
        user = request.user
        suggest_audit_list = Suggest.objects.filter(state=2, current_node__flow_role__users__exact=user)
        return render(request, 'suggest/suggest_audit_list.html', context={
            'suggest_audit_list': suggest_audit_list
        })


class SuggestAuditedListView(View):
    @staticmethod
    @login_required
    def get(request):
        user = request.user
        # suggest_audited_list = Suggest.objects.filter()
        suggest_audit_list = SuggestAudit.objects.filter(audit_user=user)
        suggest_list = []
        for suggest_audit in suggest_audit_list:
            suggest = suggest_audit.suggest
            suggest_list.append(suggest)
        suggest_list = list(set(suggest_list))
        return render(request, 'suggest/suggest_audit_list.html', context={
            'suggest_audit_list': suggest_list
        })


class ProjectView(View):
    @staticmethod
    @login_required
    def get(request):
        obj = ProjectForm()
        return render(request, 'suggest/project.html', context={
            'obj': obj
        })

    @staticmethod
    @login_required
    def post(request):
        obj = ProjectForm(request.POST)
        if obj.is_valid():
            values = obj.cleaned_data
            return HttpResponse('success')
        else:
            return render(request, 'suggest/project.html', context={
                'obj': obj
            })


class ProjectRecordView(View):
    pass
